<?php
function __autoload($url){
	$url=strtolower($url);
	$str=str_replace("_","/",$url);
	$str=str_replace("model","models",$str);
	$str=str_replace("helper","helpers",$str);
	require("$str.php");
	
}

function redirect($url){
	header("location:$url");
	exit();
}

function loadview($url,$data=""){
	require("views/$url.php");
}

function uploadImage($inputName, $uploadDir, $uploadDirThumb)
{
	$image = $_FILES[$inputName];
	$imagePath = '';
	if (trim($image['tmp_name']) != '')
	{
		$ext = substr(strrchr($image['name'], "."), 1); 
		$imagePath=md5(rand() * time()).".$ext";
		$extension=get_image_extension($imagePath);
		if(($extension != "jpg") && ($extension != "jpeg") && 
		($extension != "png") && ($extension != "gif") )
		{
		exit();
		} else {
		$move=@copy($image['tmp_name'], $uploadDir . $imagePath);
		if($move){
		create_thumbnail($uploadDir.$imagePath,$uploadDirThumb.$imagePath,20,20);
		} else {
		exit();
		}
		}
	}
return $imagePath;
}


function create_thumbnail($source, $destination, $thumb_width, $thumb_height)	{
	$size=getimagesize($source);
	$width=$size[0];
	$height=$size[1];
	$x=0;	$y=0;

	$new_image=imagecreatetruecolor($thumb_width,$thumb_height) or die();
	$extension=get_image_extension($source);
	if($extension=="jpg" || $extension=="jpeg")
		$image=imagecreatefromjpeg($source);
	if($extension=="gif")
		$image=imagecreatefromgif($source);
	if($extension=="png")
		$image=imagecreatefrompng($source);
	imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_height,$width,$height);
	if($extension=="jpg" || $extension=="jpeg")
		imagejpeg($new_image,$destination);
	if($extension=="gif")
		imagegif($new_image,$destination);
	if($extension=="png")
		imagepng($new_image,$destination);
}
function get_image_extension($name)	{
	$name=strtolower($name);
	$i=strrpos($name,".");
	if(!$i){	return "";	}
	$l=strlen($name)-$i;
	$extension=substr($name,$i+1,$i);
	return $extension;
}

